package com.lsk.serviceDriverUser.controller;

import com.lsk.internalcommon.DTO.DriverUser;
import com.lsk.internalcommon.DTO.ResponseResult;
import com.lsk.internalcommon.constant.DriverCarConstants;
import com.lsk.internalcommon.response.DriverUserIfExistResponse;
import com.lsk.serviceDriverUser.service.DriverUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class DriverUserController {

    @Autowired
    private DriverUserService driverUserService;

    @GetMapping("/check-driver/{driverPhone}")
    public ResponseResult<DriverUserIfExistResponse> getDriverUserByPhone(@PathVariable("driverPhone") String driverPhone){

        ResponseResult<DriverUser> responseResult = driverUserService.getDriverUserByPhone(driverPhone);
        DriverUser driverUserDB = responseResult.getData();
        //包装返回值
        DriverUserIfExistResponse response = new DriverUserIfExistResponse();
        if (driverUserDB ==null){
            response.setDriverPhone(driverPhone);
            response.setIfExist(DriverCarConstants.DRIVER_NOT_EXIST);
        }else {
            response.setDriverPhone(driverUserDB.getDriverPhone());
            response.setIfExist(DriverCarConstants.DRIVER_EXIST);
        }

        return ResponseResult.success(response);
    }

    @PostMapping("/driver-user")
    public ResponseResult addDriverUser(@RequestBody DriverUser driverUser){
        return driverUserService.addDriverUser(driverUser);
    }

    @PutMapping("/driver-user")
    public ResponseResult updateDriverUser(@RequestBody DriverUser driverUser){
        return driverUserService.updateDriverUser(driverUser);
    }

}
